package com.mayisoftware.mayioa.business.personal.impl.serviceImpl;

import com.mayisoftware.mayioa.business.personal.api.domain.PersonalSign;
import com.mayisoftware.mayioa.business.personal.api.service.IPersonalSignService;
import com.mayisoftware.mayioa.business.personal.impl.dao.PersonalSignMapper;
import com.mayisoftware.mayioa.business.pool.api.domain.PoolMgrInfo;
import com.mayisoftware.mayioa.business.pool.api.service.IPoolManualService;
import com.mayisoftware.mayioa.platform.impl.controller.system.SysProfileController;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 报名信息查询服务实现类
 */
@Service
public class PersonalSignServiceImpl implements IPersonalSignService {

    /**
     * 骨干池管理服务
     */
    @Autowired
    private IPoolManualService poolManualService;
    /**
     * 查询报名信息dao
     */
    @Autowired
    private PersonalSignMapper personalSignMapper;

    @Override
    public List<PersonalSign> querySignListByUserId(Integer userId) {
        return personalSignMapper.selectSignListByUserId(userId);
    }
    @Override
    public int updateUser(PersonalSign sign){
        return personalSignMapper.updateUser(sign);
    }
    @Override
    public int addSignInfo(PersonalSign sign) {

       Integer mgrRecId = -1;

       Long curId = null;

       try{
            curId = ShiroUtils.getUserId();
       }
       catch (Exception e){
           curId = 1L;
       }

        List<PoolMgrInfo> poolMgrInfos=  poolManualService.queryPoolListByUserId(sign.getPerSignUserId().toString(),curId);

        boolean hasMgr = false;
        if (CollectionUtils.isEmpty(poolMgrInfos)){
            hasMgr =false;
        }
        else {
            for (PoolMgrInfo info:poolMgrInfos){
                if (info.getPoolPlanId().equals(sign.getPerSignPlanId())){
                    mgrRecId = info.getPoolRecordId();
                    hasMgr =true;
                    break;
                }
            }
        }

        if (!hasMgr){
            PoolMgrInfo mgrInfo = new PoolMgrInfo();
            mgrInfo.setPoolUpdateTime(new Date());
            mgrInfo.setPoolScoreStatus(0);
            mgrInfo.setPoolPlanId(sign.getPerSignPlanId());
            mgrInfo.setPoolIsBackbone(0);
            mgrInfo.setPoolUserId(sign.getPerSignUserId());
            mgrInfo.setPoolTotalScore(BigDecimal.valueOf(0));
            mgrInfo.setPoolCreateType(0);
            poolManualService.addPoolInfo(mgrInfo);
        }else {
            PoolMgrInfo mgrInfo = new PoolMgrInfo();
            mgrInfo.setPoolUpdateTime(new Date());
            mgrInfo.setPoolScoreStatus(0);
            mgrInfo.setPoolRecordId(mgrRecId);
            poolManualService.modifyPoolInfo(mgrInfo);
        }

        return personalSignMapper.insertSignInfo(sign);
    }

    @Override
    public boolean isPlanJoined(Integer planId) {
        List<PersonalSign> personalSigns = personalSignMapper.selectASingByPlanId(planId);

        if (CollectionUtils.isEmpty(personalSigns)){
            return false;
        }

        return true;
    }

    @Override
    public List<PersonalSign> queryApplyInfo(PersonalSign personApply) {
        return personalSignMapper.selectApplyInfo(personApply);
    }
}
